<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1200" destroy-on-close
						 top="0"
						 @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-row>
			<el-col :span="12">
			<el-form-item label="分类" prop="category_id">
				<el-cascader v-model="form.category_id" :options="groups" :props="groupsProps" :show-all-levels="false"
										 clearable style="width: 100%;"></el-cascader>
			</el-form-item>
			</el-col>
			<el-col :span="12">
			<el-form-item label="排序" prop="sort">
				<el-input v-model="form.sort" placeholder="" clearable></el-input>
			</el-form-item>
			</el-col>
			</el-row>
			<el-form-item label="标题" prop="title">
				<el-input v-model="form.title" clearable></el-input>
			</el-form-item>
			<el-form-item label="封面" prop="cover">
				<sc-upload-multiple ref="cover" v-model="form.cover" title="选择封面"></sc-upload-multiple>
			</el-form-item>
			<el-form-item label="跳转URL" prop="jump_url">
				<el-input v-model="form.jump_url" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import api from '@/api/index.js'
import tool from '@/utils/tool.js'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
        apiObj: api.cms.slides,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:0,
					category_id:0,
					sort:0,
					title:'',
					cover:'',
					jump_url:'',
				},
				//验证规则
				rules: {
					cat_name: [
						{required: true, message: '请输入名称'}
					]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					label: "cat_name",
					emitPath: false,
					checkStrictly: true
				},
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			async getGroup(){
				var res = await api.cms.slidesCategory.list();
				this.groups = tool.deepTree(res.data,'parent_id')
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						let formData = {}
						Object.assign(formData, this.form)
						let methodName;
						if (formData.id > 0) {
							methodName = 'update'
						}
						else {
							methodName = 'add'
						}
						let res = await this.apiObj[methodName](formData);
						this.isSaveing = false;
						if(res.code === 0){
							this.$emit('success', formData, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			async getInfoData(id) {
				this.isSaveing = true;
				let res = await this.apiObj.info({id:id});
				this.isSaveing = false;
				if(res.code === 0){
					this.form = res.data
					if (res.data.photo_base_url) {
						this.$refs.cover.setBaseUrl(res.data.photo_base_url)
					}
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//表单注入数据
			async setData(data){
				this.getInfoData(data.id)
			}
		}
	}
</script>

<style scoped>
.treeMain {height:280px;overflow: auto;border: 1px solid #dcdfe6;margin-bottom: 10px;}
</style>
